import request from '@/utils/request'
import store from '@/store'

// =================== 本地单相关API ===================
export function exportLocalOrder(data) {
  return request({
    url: '/v1/shipping/local/export',
    method: 'post',
    data,
    responseType: 'blob'
  })
}

export function fetchLocalList(params) {
  return request({
    url: '/v1/shipping/local',
    method: 'get',
    params
  }).then(response => {
    let items = []
    
    if (response.results && response.results.data && response.results.data.items) {
      items = response.results.data.items
    }
    
    if (items && items.length > 0) {
      // 提取所有fs_no
      const fsNos = items.map(item => item.fs_no)
      // 如果有fs_no，则批量查询配送方式
      if (fsNos.length > 0) {
        return getShippingMethod({
          fs_no: fsNos
        }).then(methodResponse => {
          // 将配送方式信息合并到原始数据中
          return {
            ...response,
            shipping_methods: methodResponse.data
          }
        })
      }
    }
    return response
  })
}

export function deleteLocalOrder(id) {
  return request({
    url: '/v1/shipping/local/delete_order',
    method: 'post',
    data: { id }
  })
}

export function importLocalOrders(data) {
  return request({
    url: '/v1/shipping/local/import',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

export function updateLocalOrder(data) {
  return request({
    url: `/v1/shipping/local/update`,
    method: 'put',
    data
  })
}

export function batchDeleteLocalOrders(fsNos) {
  // 发送一个post请求，请求地址为'/v1/shipping/local/batch_delete'，请求参数为fsNos
  return request({
    url: '/v1/shipping/local/batch_delete',
    method: 'post',
    data: { fs_no: fsNos }
  })
}

export function batchUpdateLocalOrdersDate(fsNos, createTime) {
  // 批量更新本地单创建日期
  return request({
    url: '/v1/shipping/local/batch_update_date',
    method: 'post',
    data: { 
      fs_no: fsNos,
      create_time: createTime
    }
  })
}

// =================== 转运单相关API ===================
export function exportTransferOrders(data) {
  return request({
    url: '/v1/shipping/transfer/export',
    method: 'post',
    data,
    responseType: 'blob'
  })
}

// 生成转运数据PDF
export function generateTransferPDF(orders) {
  return request({
    url: '/v1/shipping/transfer/generate-pdf',
    method: 'post',
    data: orders,
    responseType: 'blob' // 接收二进制数据
  })
}

// 保存转运数据PDF到后台
export function saveTransferPDF(orders) {
  return request({
    url: '/v1/shipping/transfer/save-pdf',
    method: 'post',
    data: orders
  })
}

// 获取保存的PDF列表
export function getSavedPDFs(params) {
  return request({
    url: '/v1/shipping/transfer/saved-pdfs',
    method: 'get',
    params: params
  })
}

// 删除单个PDF文件
export function deletePDF(data) {
  return request({
    url: '/v1/shipping/transfer/delete-pdf',
    method: 'delete',
    data: data
  })
}

// 批量删除PDF文件
export function batchDeletePDFs(data) {
  return request({
    url: '/v1/shipping/transfer/batch-delete-pdfs',
    method: 'post',
    data: data
  })
}

// 批量下载PDF文件（ZIP）
export function batchDownloadPDFs(data) {
  return request({
    url: '/v1/shipping/transfer/batch-download-pdfs',
    method: 'post',
    data: data,
    responseType: 'blob'
  })
}

export function fetchTransferList(query) {
  return request({
    url: '/v1/shipping/transfer',
    method: 'get',
    params: query
  })
}

export function updateTransferOrder(data) {
  return request({
    url: '/v1/shipping/transfer/update',
    method: 'put',
    data
  })
}

export function deleteTransferOrder(id) {
  return request({
    url: `/v1/shipping/transfer/delete_order`,
    method: 'post',
    data: { id }
  })
}

export function batchDeleteTransferlOrders(cwNos) {
  return request({
    url: '/v1/shipping/transfer/batch_delete',
    method: 'post',
    data: { cw_no: cwNos }
  })
}

export function batchUpdateTransferOrdersDate(cwNos, createTime) {
  // 批量更新转运单创建日期
  return request({
    url: '/v1/shipping/transfer/batch_update_date',
    method: 'post',
    data: { 
      cw_no: cwNos,
      create_time: createTime
    }
  })
}

export function importTransferOrders(data) {
  return request({
    url: '/v1/shipping/transfer/import',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// =================== 售后单相关API ===================
export function fetchAftersaleList(params) {
  return request({
    url: '/v1/shipping/aftersale',
    method: 'get',
    params
  })
}

export function importAftersaleOrder(data) {
  return request({
    url: '/v1/shipping/aftersale/import',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

export function exportAftersaleOrder(data) {
  return request({
    url: '/v1/shipping/aftersale/export',
    method: 'post',
    data,
    responseType: 'blob'
  })
}

export function updateAftersaleOrder(data) {
  return request({
    url: '/v1/shipping/aftersale/update',
    method: 'put',
    data
  })
}

export function deleteAftersaleOrder(id) {
  return request({
    url: `/v1/shipping/aftersale/delete_order`,
    method: 'post',
    data: { id }
  })
}

// =================== 调仓单相关API ===================
// 获取调仓单列表
export function fetchRelocationList(query) {
  return request({
    url: '/v1/shipping/relocation',
    method: 'get',
    params: query
  })
}

// 创建调仓单
export function importRelocationOrder(data) {
  return request({
    url: '/v1/shipping/relocation/import',
    method: 'post',
    data
  })
}

// 更新调仓单
export function updateRelocationOrder(data) {
  return request({
    url: '/v1/shipping/relocation/update',
    method: 'put',
    data
  })
}

// 删除调仓单
export function deleteRelocationOrder(id) {
  return request({
    url: '/v1/shipping/relocation/delete_order',
    method: 'post',
    data: { id }
  })
}

// =================== 跨仓单相关API ===================
// 获取跨仓单列表
export function fetchCrossList(query) {
  return request({
    url: '/v1/shipping/cross',
    method: 'get',
    params: query
  })
}

// 导入跨仓单
export function importCrossOrder(data) {
  return request({
    url: '/v1/shipping/cross/import',
    method: 'post',
    data
  })
}

// 更新跨仓单
export function updateCrossOrder(data) {
  return request({
    url: '/v1/shipping/cross/update',
    method: 'put',
    data
  })
}

// 删除跨仓单
export function deleteCrossOrder(id) {
  return request({
    url: '/v1/shipping/cross/delete_order',
    method: 'post',
    data: { id }
  })
}

// =================== 回国单相关API ===================
// 获取回国单列表
export function fetchReturnList(query) {
  return request({
    url: '/v1/shipping/return',
    method: 'get',
    params: query
  })
}

// 导入回国单
export function importReturnOrder(data) {
  return request({
    url: '/v1/shipping/return/import',
    method: 'post',
    data
  })
}

// 更新回国单
export function updateReturnOrder(data) {
  return request({
    url: '/v1/shipping/return/update',
    method: 'put',
    data
  })
}

// 删除回国单
export function deleteReturnOrder(id) {
  return request({
    url: '/v1/shipping/return/delete_order',
    method: 'post',
    data: { id }
  })
}

// =================== 共用API ===================
export function getRemark() {
  return request({
    url: '/v1/shipping/remark',
    method: 'get'
  })
}

export function searchPostalCode(data) {
  // data格式: [{fs_no: 'FS241201251516', postal_code: '1330052'}, ...]
  return request({
    url: '/v1/shipping/postal_code/search',
    method: 'post',
    data
  })
}

export function getShippingMethod(data) {
  return request({
    url: '/v1/shipping/shipping_method',
    method: 'post',
    data
  })
}

export function searchTrackingNumber(fsNos) {
  return request({
    url: '/v1/shipping/search/tracking/number',
    method: 'post',
    data: {
      fs_no: Array.isArray(fsNos) ? fsNos : [fsNos],
      password: store.state.user.password
    },
    timeout: 0 // 设置为0表示没有超时限制
  })
}

// =================== 库存收货相关API ===================
// 获取库存收货单列表
export function fetchInventoryReceiveList(query) {
  return request({
    url: '/v1/shipping/inventory-receive',
    method: 'get',
    params: query
  })
}

// 导入换货单
export function importInventoryReceiveOrder(data) {
  return request({
    url: '/v1/shipping/inventory-receive/import',
    method: 'post',
    data
  })
}

// 更新库存收货单
export function updateInventoryReceiveOrder(data) {
  return request({
    url: '/v1/shipping/inventory-receive/update',
    method: 'put',
    data
  })
}

// 删除库存收货单
export function deleteInventoryReceiveOrder(id) {
  return request({
    url: '/v1/shipping/inventory-receive/delete_order',
    method: 'post',
    data: { id }
  })
}

// =================== 维修单相关API ===================
// 获取维修单列表
export function fetchRepairList(query) {
  return request({
    url: '/v1/shipping/repair',
    method: 'get',
    params: query
  })
}

// 导入维修单
export function importRepairOrder(data) {
  return request({
    url: '/v1/shipping/repair/import',
    method: 'post',
    data
  })
}

// 更新维修单
export function updateRepairOrder(data) {
  return request({
    url: '/v1/shipping/repair/update',
    method: 'put',
    data
  })
}

// 删除维修单
export function deleteRepairOrder(id) {
  return request({
    url: '/v1/shipping/repair/delete_order',
    method: 'post',
    data: { id }
  })
}

// =================== 报表相关API ===================
// 获取报表数据
export function fetchReport(params) {
  return request({
    url: '/v1/shipping/report',
    method: 'get',
    params: {
      ...params
    }
  })
}

// 获取运费
export function fetchFee(params) {
  return request({
    url: '/v1/shipping/fee',
    method: 'get',
    params: { ...params }
  })
}

// 创建运费配置
export function createFeeConfig(data) {
  return request({
    url: '/v1/shipping/fee',
    method: 'post',
    data
  })
}

// 更新运费配置
export function updateFeeConfig(data) {
  return request({
    url: `/v1/shipping/fee/${data.id}`,
    method: 'put',
    data
  })
}

// 删除运费配置
export function deleteFeeConfig(id) {
  return request({
    url: `/v1/shipping/fee/${id}`,
    method: 'delete'
  })
}

// 批量删除运费配置
export function batchDeleteFeeConfig(ids) {
  return request({
    url: '/v1/shipping/fee/batch_delete',
    method: 'post',
    data: { ids }
  })
}

// 获取每日报表数据
export function fetchReportDaily(params) {
  return request({
    url: '/v1/shipping/report/daily',
    method: 'get',
    params: {
      ...params
    }
  })
}

// 导出Excel
export function exportExcel(params) {
  return request({
    url: '/v1/shipping/report/export/excel',
    method: 'get',
    responseType: 'blob',
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    params: {
      ...params
    }
  })
}

// 处理未处理订单
export function handleUnprocessedOrders(data) {
  return request({
    url: '/v1/shipping/report/unprocessed-orders',
    method: 'post',
    data
  })
}

// 获取未处理订单
export function fetchUnprocessedOrders(params) {
  return request({
    url: '/v1/shipping/report/unprocessed-orders',
    method: 'get',
    params
  })
}


// =================== 订单问答相关API ===================
// 获取订单问答列表
export function fetchOrderQAList(params) {
  return request({
    url: '/v1/shipping/order-qa',
    method: 'get',
    params
  })
}

// 创建订单问答
export function createOrderQA(data) {
  return request({
    url: '/v1/shipping/order-qa',
    method: 'post',
    data
  })
}

// 更新订单问答
export function updateOrderQA(data) {
  return request({
    url: `/v1/shipping/order-qa/${data.id}`,
    method: 'put',
    data
  })
}

// 删除订单问答
export function deleteOrderQA(id) {
  return request({
    url: `/v1/shipping/order-qa/${id}`,
    method: 'delete'
  })
}

// 获取订单问答统计信息
export function getOrderQAStatistics(params) {
  return request({
    url: '/v1/shipping/order-qa/statistics',
    method: 'get',
    params
  })
}
